<?php
/***************************************************************
*  Copyright notice
*  (c) 2009, jianyuzhu@gmail.com
*  All rights reserved
*  This script is part of the PPEMI project.
***************************************************************/

//
$config = array(
		//
		'cname' => 'item',
		
		//
		'class_file' => 'module/form/form.data.class.php',
		'class_name' => 'FormDataBase',
		'class_param' => array('_table_status_usable' => false),
		
		//
		'tpl_type' => 'admin',
		'tpl_file' => 'pagelayout/pagelayout_admin_form_data.tpl',
		
		//
		'validate_file' => '',
		'validate_class' => 'CommonValidate',
		'validate_fields' => array('field' => 'name', 'valid' => 'null'),
			);

//
include_once(CONFIG_PATH . CONFIG_DIR_CLASSES . 'control/item.control.class.php');
include_once(CONFIG_PATH . CONFIG_DIR_CLASSES . 'control/item.content.control.class.php');

//
class _ControlItemContentBase extends ControlItemContentBase {
	
	//constructor
	function _ControlItemContentBase($config = '') {
		parent::ControlItemContentBase($config);
		
		//
		if( $this->feObj->operation == '' ) {
			$this->feObj->operation = 'list';	
		}
		//
		if( $this->feObj->operation == 'X' ) {
			$this->actionHandler = 'doXActionHandler';
		}
		//
		if( $this->feObj->operation == 'detail' ) {
			$this->displayHandler = 'doFormDetailDisplayHandler';
		} elseif( $this->feObj->operation == 'list' ) {
			$this->displayHandler = 'doFormListDisplayHandler';
		}
	}
	
	//methods
	function doActionHandler() {
		echo 'doActionHandler';
		exit;
	}
	
	function doXActionHandler($obj) {
		
		_dms(':admin:success');
		$this->_redirect('', array('op'), 's');
	}
	
	//
	function doDisplayHandler() {
		echo 'doDisplayHandler';
		exit;
	}
	
	function doFormDetailDisplayHandler($obj) {
		$id = $this->feObj->_request['id'];
		$fid = $this->feObj->_request['fid'];
		if( $fid > 0 ) {
			//form id
			//
			include_once(CONFIG_PATH . CONFIG_DIR_CLASSES . 'module/form/form.class.php');
			$fObj = new FormBase();
			$fObj->format = true;
			$form = $fObj->getItem($fid, 'id');
			
			//
			include_once(CONFIG_PATH . CONFIG_DIR_CLASSES . 'module/form/form.data.class.php');
			$fdObj = new FormDataBase();
			$fdObj->format = true;
			
			//
			$filter = $this->_get_items_filter();
			$limit_length = $this->feObj->limit_length;
			$limit_start = ($this->feObj->limit_page - 1) * $limit_length;
			$limit_total = $fdObj->getItemsTotal($filter, -1);
			//
			$fdObj->setQueryLimitStart($limit_start);
			$fdObj->setQueryLimitLength($limit_length);
			//$items = $fdObj->getItems($filter, $check, $limit);
			$form_values = $fdObj->getFormValues($form['id'], '', -1, true);
			//
			$parameters = util_get_all_get_params(array('page'));
			$link_array = array(
						'page' => '',
						'parameters' => $parameters,
							);
			$navigation = util_common_navigation($link_array, $limit_total, $limit_start, $limit_length);
			
			//
			$this->feObj->content_wrap['mainpart'] = 'formdetail';
			$this->feObj->content_wrap['form'] = $form;
			$this->feObj->content_wrap['form_values'] = $form_values;
			$this->feObj->content_wrap['navigation'] = $navigation;
		} elseif( $id > 0 ) {
			//form value id
			//
			include_once(CONFIG_PATH . CONFIG_DIR_CLASSES . 'module/form/form.data.class.php');
			$fdObj = new FormDataBase();
			$fdObj->format = true;
			$form_value = $fdObj->getFormValue($id);
			
			//
			include_once(CONFIG_PATH . CONFIG_DIR_CLASSES . 'module/form/form.class.php');
			$fObj = new FormBase();
			$fObj->format = true;
			$form = $fObj->getItem($form_value['value']['fid'], 'id');
			
			//
			$this->feObj->content_wrap['mainpart'] = 'datadetail';
			$this->feObj->content_wrap['form'] = $form;
			$this->feObj->content_wrap['form_value'] = $form_value;
		}
	}
	
	function doFormListDisplayHandler($obj) {
		//
		include_once(CONFIG_PATH . CONFIG_DIR_CLASSES . 'module/form/form.class.php');
		$fObj = new FormBase();
		$fObj->format = true;
		
		//
		$filter = $this->_get_items_filter();
		$limit_length = $this->feObj->limit_length;
		$limit_start = ($this->feObj->limit_page - 1) * $limit_length;
		$limit_total = $fObj->getItemsTotal($filter, -1);
		//
		$fObj->setQueryLimitStart($limit_start);
		$fObj->setQueryLimitLength($limit_length);
		$items = $fObj->getItems($filter, $check, $limit);
		//
		$parameters = util_get_all_get_params(array('page'));
		$link_array = array(
					'page' => '',
					'parameters' => $parameters,
						);
		$navigation = util_common_navigation($link_array, $limit_total, $limit_start, $limit_length);
		
		//
		$this->feObj->content_wrap['mainpart'] = 'list';
		$this->feObj->content_wrap['items'] = $items;
		$this->feObj->content_wrap['navigation'] = $navigation;
	}
	
	//
	function _doAfter() {
		
	}
}

//
$cntlObj = new _ControlItemContentBase($config);
$cntlObj->process();
//
?>